package com.llkj.qingnianguoxue.music;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.bean.CommentResponseBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.RxBus;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PublicCommentActivity extends BaseActivity {

    @BindView(R.id.etInputComment)
    protected EditText etInputComment;//评论内容
    @BindView(R.id.tvTextCount)
    protected TextView tvTextCount;//内容大小
    @BindView(R.id.btnPublish)
    protected TextView btnPublish;//发布按钮
    private String mMusicId;//音乐id
    private int mCommentType;//1 课程大师 2 伴奏 3 视频

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_public_comment);
        setTitle("写评论", true, TITLE_TYPE_IMG, R.drawable.icon_back, false,-1,-1);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);

        ButterKnife.bind(this);
        Intent intent = getIntent();
        mMusicId = intent.getStringExtra("musicId");
        mCommentType = intent.getIntExtra("commentType",2);

        tvTextCount.setText(String.format(getString(R.string.comment_input_count), String.valueOf(0)));
        initListener();
    }

    private void initListener(){
        etInputComment.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String strLength = String.valueOf(charSequence.length());
                tvTextCount.setText(String.format(getString(R.string.comment_input_count),strLength));
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @OnClick(R.id.btnPublish)
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btnPublish:
                String content = etInputComment.getText().toString();
                if(StringUtils.isEmpty(content)){
                    ToastUtil.makeLongText(this,"请输入评论内容");
                    return;
                }

                loadWaitDialog();
                HttpMethod.kojiComment(this,this,content,mMusicId,String.valueOf(mCommentType));
                break;
        }
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (HttpMethod.HTTP_KOJI_COMMENT == method) {
            setResult(RESULT_OK);

            EventBus.getDefault().post(new CommentResponseBean(true));
            myFinish();
        }
    }

    @Override
    protected void myToast() {
        super.myToast();
        ToastUtil.makeLongText(this,"评论成功");
    }

    public static void startActivity(Activity activity, String musicId){
        Intent intent = new Intent(activity,PublicCommentActivity.class);
        intent.putExtra("musicId",musicId);
        activity.startActivity(intent);
    }

    public static void startActivity(Activity activity, String musicId,int commentType){
        Intent intent = new Intent(activity,PublicCommentActivity.class);
        intent.putExtra("musicId",musicId);
        intent.putExtra("commentType",commentType);
        activity.startActivity(intent);
    }
}
